/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-getCar
*
* @File name : GetCarController.java
*
* @Author : Administrator
*
* @Date : 2017年6月30日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年6月30日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/
	
package com.yonyou.gmmc.service.getCar.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.util.CheckUtil;
import org.marketing.common.util.MapConverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.common.annotation.ParamValid;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.getCar.constant.GetCarConstant;
import com.yonyou.gmmc.service.getCar.entity.GscReservation;
import com.yonyou.gmmc.service.getCar.entity.IdrealerStatus;
import com.yonyou.gmmc.service.getCar.service.GetCarService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
*
* @author Administrator
* TODO description
* @date 2017年6月30日
*/
@RestController
@RequestMapping(value = GetCarConstant.BASE_PATH + "/" + GetCarConstant.VERSION)
@Api("取送车API")
public class GetCarController {

    private Logger logger = Logger.getLogger(GetCarController.class);
    @Autowired
    private GetCarService getCarService;
    
    /**
	 * 判断是否有取送车权限
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/isCanGetSendCar", method = RequestMethod.GET)
	@ApiOperation(value = "判断是否有取送车权限", notes = "判断是否有取送车权限")
	@ParamValid
	public Response<Map<String, Object>> isCanGetSendCar(@RequestParam(required=false,defaultValue="") String dealerCode) {
		logger.info("GetCarController.isCanGetSendCar==>start...");
		try {
			Map<String, Object> list = getCarService.isCanGetSendCar(dealerCode);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.isCanGetSendCar==>end...");
		}
	}
	/**
	 * 向Idealer下单，积分支付
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/saveGscReservation", method = RequestMethod.GET)
	@ApiOperation(value = "向Idealer下单，积分支付", notes = "向Idealer下单，积分支付")
	@ParamValid
	public Response<Map<String, Object>> saveGscReservation(@RequestParam(required=true,defaultValue="") String pkId) {
		logger.info("GetCarController.saveGetOrSendCar==>start...");
		try {
			Map<String, Object> map = getCarService.saveGetOrSendCar(pkId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.saveGetOrSendCar==>end...");
		}
	}
	/**
	 * 向Idealer下单，微信支付
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/saveWxOrder", method = RequestMethod.GET)
	@ApiOperation(value = "向idealer下单，并微信支付", notes = "向idealer下单，并微信支付")
	@ParamValid
	public Response<Map<String, Object>> saveWxOrder(@RequestParam(required=true,defaultValue="") String pkId) {
		logger.info("GetCarController.saveWxOrder==>start...");
		try {
			Map<String, Object> map = getCarService.saveWxOrder(pkId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.saveWxOrder==>end...");
		}
	}
	/**
	 * 向Idealer下单，微信支付成功，状态更新
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/updateWxState", method = RequestMethod.GET)
	@ApiOperation(value = "微信支付成功，状态更新", notes = "微信支付成功，状态更新")
	@ParamValid
	public Response<Map<String, Object>> updateWxState(@RequestParam(required=true,defaultValue="") String pkId
			,@RequestParam(required=true,defaultValue="") String sign) {
		logger.info("GetCarController.updateWxState==>start...");
		try {
			Map<String, Object> map = getCarService.updateWxState(pkId,sign);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.updateWxState==>end...");
		}
	}
	/**
	 * 取消取送车
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/cancelCar", method = RequestMethod.GET)
	@ApiOperation(value = "取消取送车", notes = "取消取送车")
	public Response<Map<String, Object>> cancelCar(@RequestParam(required=false,defaultValue="") String etcoId) {
		logger.info("GetCarController.cancelGetSendCar==>start...");
		try {
			
			Map<String, Object> map = getCarService.cancelGetSendCar(etcoId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.cancelGetSendCar==>end...");
		}
	}
	/**
	 * 取送车 快捷获取地址
	 * @param searchType  1:首选销售店   2:家  0:地图输入地址
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/searchAddr", method = RequestMethod.GET)
	@ApiOperation(value = "快捷获取地址", notes = "快捷获取地址")
	@ParamValid
	public Response<Map<String, Object>> searchAddr(@RequestParam(required=false,defaultValue="1") Integer searchType //1 首选经销商   2家
			,@RequestParam(value = "userId_", required = false) String userId_
			,HttpServletRequest request) {
		logger.info("GetCarController.searchAddr==>start...");
		try {
			String userId="";
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
	    	}else{
	    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
	    	}
			Map<String, Object> map = getCarService.searchAddr(searchType,userId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.searchAddr==>end...");
		}
	}
	/**
	 * 取送车 获取历史地址
	 * @param addrName  搜索地址名称
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/searchHistory", method = RequestMethod.GET)
	@ApiOperation(value = "获取历史地址", notes = "获取历史地址")
	@ParamValid
	public Response<List<Map<String, Object>>> searchHistory(@RequestParam(required=false,defaultValue="") String addrName
			,@RequestParam(value = "userId_", required = false) String userId_
			,HttpServletRequest request) {
		logger.info("GetCarController.searchHistory==>start...");
		try {
			String userId="";
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
	    	}else{
	    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
	    	}
			List<Map<String, Object>> list = getCarService.searchHistory(addrName,userId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.searchHistory==>end...");
		}
	}
	@RequestMapping(value = "/getCarInfo", method = RequestMethod.GET)
	@ApiOperation(value = "获取车主信息", notes = "获取车主信息")
	public Response<Map<String, Object>> getCarInfo(@RequestParam(value = "userId_", required = false) String userId_
			,HttpServletRequest request) {
		logger.info("GetCarController.validateGscDistance==>start...");
		try {
			String userId="";
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
	    	}else{
	    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
	    	}
			Map<String, Object> map = getCarService.getCarInfo(userId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.validateGscDistance==>end...");
		}
	}
	/**
	 * 取送车 判断距离是否超出服务范围
	 * @param addrName  搜索地址名称
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/validateGscDistance", method = RequestMethod.POST)
	@ApiOperation(value = "判断距离是否超出服务范围", notes = "判断距离是否超出服务范围")
	public Response<Map<String, Object>> validateGscDistance(@RequestBody @Valid GscReservation gscReservation
			,@RequestParam(value = "userId_", required = false) String userId_
			,HttpServletRequest request) {
		logger.info("GetCarController.validateGscDistance==>start...");
		try {
			String userId="";
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
	    	}else{
	    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
	    	}
			Map<String, Object> map = getCarService.validateGscDistance(gscReservation,userId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.validateGscDistance==>end...");
		}
	}
    /**
	 * 我的取送车  列表
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/queryCar", method = RequestMethod.GET)
	@ApiOperation(value = "查看取送（车）订单列表", notes = "查看取送（车）订单列表")
	public Response<List<Map<String, Object>>> queryCar(@RequestParam(value = "userId_", required = false) String userId_
			,HttpServletRequest request) {
		logger.info("GetCarController.queryCar==>start...");
		try {
			String userId="";
			if(!CheckUtil.NullOrEmpty(userId_)){
				userId = userId_;
	    	}else{
	    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
	    	}
			List<Map<String, Object>> list = getCarService.queryCar(userId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryCar==>end...");
		}
	}
	/**
	 * 我的取送车  详情
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/queryQscDetail", method = RequestMethod.GET)
	@ApiOperation(value = "查看取送（车）订单详情", notes = "查看取送（车）订单详情")
	public Response<Map<String, Object>> queryQscDetail(@RequestParam(required=true,defaultValue="") String etcoId) {
		logger.info("GetCarController.queryQscDetail==>start...");
		try {
			Map<String, Object> map = getCarService.queryQscDetail(etcoId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryQscDetail==>end...");
		}
	}
	/**
	 * 我的取送车  查看进度
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/queryQscProgress", method = RequestMethod.GET)
	@ApiOperation(value = "查看进度", notes = "查看进度")
	public Response<Map<String, Object>> queryQscProgress(@RequestParam(required=true,defaultValue="") String etcoId) {
		logger.info("GetCarController.queryQscProgress==>start...");
		try {
			Map<String, Object> map = getCarService.queryQscProgress(etcoId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryQscProgress==>end...");
		}
	}
	/**
	 * 我的取送车  查看轨迹信息
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/queryQscTrack", method = RequestMethod.GET)
	@ApiOperation(value = "查看轨迹信息", notes = "查看轨迹信息")
	public Response<Map<String, Object>> queryQscTrack(@RequestParam(required=true,defaultValue="") String etcoId) {
		logger.info("GetCarController.queryQscTrack==>start...");
		try {
			Map<String, Object> map = getCarService.queryQscTrack(etcoId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryQscTrack==>end...");
		}
	}
	/**
	 * 我的取送车  查看司机信息
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/queryDriverinfo", method = RequestMethod.GET)
	@ApiOperation(value = "查看司机信息", notes = "查看司机信息")
	public Response<Map<String, Object>> queryDriverinfo(@RequestParam(required=true,defaultValue="") String etcoId) {
		logger.info("GetCarController.queryDriverinfo==>start...");
		try {
			Map<String, Object> map = getCarService.queryDriverinfo(etcoId);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("GetCarController.queryDriverinfo==>end...");
		}
	}
	/**
	 * 我的取送车  更新订单进度和司机进度
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/qscStatus", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "更新订单进度和司机进度", notes = "更新订单进度和司机进度")
	public @ResponseBody Map<String, Object> qscStatus(ServletRequest IdrealerStatus) {
		logger.info("GetCarController.qscStatus==>start...");
		Map<String, Object> param = MapConverUtil.RequestPO2Map(IdrealerStatus,new IdrealerStatus());
		Map<String, Object> map = getCarService.qscStatus(param);
		logger.info("GetCarController.qscStatus==>end...");
		return map;
//		try {
//			Map<String, Object> param = MapConverUtil.RequestPO2Map(IdrealerStatus,new IdrealerStatus());
//			Map<String, Object> map = getCarService.qscStatus(param);
//			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, map, "");
//		} catch (Exception e) {
//			logger.error("查询状态发生异常", e);
//			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
//		} finally {
//			logger.info("GetCarController.qscStatus==>end...");
//		}
	}
}
